/*
 * ---------------------------------------------------------------------
 * Copyright (c) 2014, 2015 All Right Reserved
 * This software is developed for study and improve coding skill ...
 *
 * Project:  Excellent Coding< EC >
 *
 * ecSemaphore.h
 * This file for ecSemaphore interface implementation.
 *
 * Eamil:   epengao@126.com
 * Author:  Peter Gao
 * Version: Intial first version.
 * --------------------------------------------------------------------
 */

#include "ecSemaphore.h"

namespace ec {

ecSemaphore::ecSemaphore(EC_U32 nInitSig, /*0*/
                         EC_U32 nMaxSig   /*EC_MAX_U16*/)
{
#ifdef _WIN32
    m_hSemaphore = CreateSemaphore
                   (
                      PCD_NULL,  // default security attributes
                      nInitSig,  // initial count
                      nMaxSig,   // maximum count
                      PCD_NULL   // unnamed semaphore
                   )
#else
    sem_init(&m_hSemaphore, 0, nInitSig);
#endif
}

ecSemaphore::~ecSemaphore()
{
#ifdef _WIN32
    CloseHandle(m_hSemaphore);
#else
    sem_destroy(&m_hSemaphore);
#endif
}

void ecSemaphore::SemAddSIGNAL()
{
#ifdef _WIN32
    ReleaseSemaphore(m_hSemaphore, 1, EC_NULL);
#else
    sem_post(&m_hSemaphore);
#endif
}

void ecSemaphore::SemRemoveSIGNAL()
{
#ifdef _WIN32
    WaitForSingleObject(m_hSemaphore,INFINITE);
#else
    sem_wait(&m_hSemaphore); 
#endif
}

} /* namespace ec */
